//
//  ZBPathAnimationView.m
//  送礼
//
//  Created by 华惠友 on 2017/9/19.
//  Copyright © 2017年 华惠友. All rights reserved.
//


#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

#import "ZBPathAnimationView.h"

@interface ZBPathAnimationView ()<CAAnimationDelegate> 

@property (nonatomic, strong) UIImageView *imageVeiw;
@property (nonatomic, copy) AnimationFinishBlock finishedBlock;

@end

@implementation ZBPathAnimationView

static ZBPathAnimationView *_animationView = nil;

+ (instancetype)sharedInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _animationView = [[self alloc] init];
    });
    return _animationView;
}

//- (instancetype)init {
//    self = [super init];
//    if (self) {
//        self.frame = CGRectMake(0, 0, 100, 60);
//        [self addSubview:self.imageVeiw];
//    }
//    return self;
//}

- (void)animationWithPath:(AnimationFinishBlock)finishedBlock {
    self.isAnimating = YES;     // 正在执行动画
    self.frame = CGRectMake(0, 0, 100, 60);
    [self addSubview:self.imageVeiw];
    
    self.finishedBlock = finishedBlock;
    
    CAKeyframeAnimation *position = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, nil, -100, 300);
    CGPathAddQuadCurveToPoint(path, nil, SCREEN_WIDTH + 100, 500, SCREEN_WIDTH+120, 350);
    
    position.path = path;
    position.duration = 5.0f;
    position.speed = 0.7;
    position.removedOnCompletion = NO;
    position.fillMode = kCAFillModeForwards;
    
    CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    scaleAnimation.duration = 5.0f;
    scaleAnimation.beginTime = 0.0f;
    scaleAnimation.fromValue = [NSNumber numberWithFloat:1.0];
    scaleAnimation.toValue = [NSNumber numberWithFloat:2.5];
    scaleAnimation.removedOnCompletion = NO;
    scaleAnimation.fillMode = kCAFillModeForwards;
    
    CAKeyframeAnimation *anima = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
    NSValue *value1 = [NSNumber numberWithFloat:-M_PI/180*8];
    NSValue *value2 = [NSNumber numberWithFloat:M_PI/180*8];
    NSValue *value3 = [NSNumber numberWithFloat:-M_PI/180*8];
    anima.values = @[value1,value2,value3];
    anima.repeatCount = MAXFLOAT;
    
    CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
    animationGroup.duration = 7.0f;
    animationGroup.removedOnCompletion = NO;
    animationGroup.fillMode = kCAFillModeForwards;
    animationGroup.delegate = self;
    animationGroup.animations = @[position,scaleAnimation,anima];
    [self.layer addAnimation:animationGroup forKey:@"shipView"];
}

#pragma mark - CAAnimationDelegate
// 动画结束移除视图
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    self.isAnimating = NO;     // 动画结束
    [self.layer removeAllAnimations];
    [self removeFromSuperview];
    
    self.finishedBlock();
    
}

- (void)dealloc {
    NSLog(@"视图销毁");
}

- (UIImageView *)imageVeiw {
    if (!_imageVeiw) {
        _imageVeiw = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ship_body"]];
        _imageVeiw.frame = self.bounds;
    }
    return _imageVeiw;
}

@end












